package com.csu.letaobackend.serviceM.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csu.letaobackend.bean.Shopcoupon;
import com.csu.letaobackend.bean.Shop;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.persistence.ShopDao;
import com.csu.letaobackend.persistence.ShopcouponDao;
import com.csu.letaobackend.serviceM.CouponMService;
import com.csu.letaobackend.vo.ShopcouponVO;
import com.csu.letaobackend.vo.ShopcouponVO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
@MapperScan("com.csu.letaobackend.persistence")
public class CouponMServiceImpl implements CouponMService {

      @Autowired
      ShopcouponDao shopcouponDao;

      @Autowired
      ShopDao shopDao;

      @Override
      public CommonResponse<IPage<ShopcouponVO>> getAllShopcoupons(int pageid, int pagesize) {

            IPage<Shopcoupon> couponIPage=new Page<>(pageid,pagesize);
            shopcouponDao.selectPage(couponIPage,null);

            List<ShopcouponVO>  couponVOList=new ArrayList<>();
            for(Shopcoupon coupon: couponIPage.getRecords()){
                  couponVOList.add(entityToVO(coupon));
            }
            IPage<ShopcouponVO> couponVOIPage=new Page<>(pageid,pagesize);
            couponVOIPage.setTotal(couponIPage.getTotal());
            couponVOIPage.setRecords(couponVOList);
            return CommonResponse.createForSuccess(couponVOIPage);
      }

      @Override
      public CommonResponse<IPage<ShopcouponVO>> searchShopcoupons(double fullminus, String shopname, int pageid, int pagesize) {
            IPage<Shopcoupon> couponIPage=new Page<>(pageid,pagesize);

            QueryWrapper<Shop> queryWrapper1 = new QueryWrapper();
            queryWrapper1.like("name",shopname);
            List<Integer> list=new ArrayList<>();
            for (Shop shop : shopDao.selectList(queryWrapper1)) {
                  list.add(shop.getShopid());
            }

            QueryWrapper<Shopcoupon> queryWrapper= new QueryWrapper<>();
            queryWrapper.ge("fullminus",fullminus).in("shopid",list);
            shopcouponDao.selectPage(couponIPage,queryWrapper);

            List<ShopcouponVO>  couponVOList=new ArrayList<>();
            for(Shopcoupon coupon: couponIPage.getRecords()){
                  couponVOList.add(entityToVO(coupon));
            }
            IPage<ShopcouponVO> couponVOIPage=new Page<>(pageid,pagesize);
            couponVOIPage.setTotal(couponIPage.getTotal());
            couponVOIPage.setRecords(couponVOList);
            return CommonResponse.createForSuccess(couponVOIPage);
      }

      @Override
      public CommonResponse addShopcoupon(Shopcoupon coupon) {
            coupon.setCouponid(getMaxCouponId()+1);
            shopcouponDao.insert(coupon);
            return CommonResponse.createForSuccessMessage("添加优惠券成功");
      }

      @Override
      public CommonResponse deleteShopcoupon(int couponid) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("couponid",couponid);
            shopcouponDao.delete(queryWrapper);
            return CommonResponse.createForSuccessMessage("删除优惠券成功");
      }

      @Override
      public CommonResponse updateShopcoupon(Shopcoupon coupon) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("couponid",coupon.getCouponid());
            shopcouponDao.update(coupon,queryWrapper);
            return CommonResponse.createForSuccessMessage("更新优惠券成功");
      }

      private ShopcouponVO entityToVO(Shopcoupon coupon) {
            ShopcouponVO shopcouponVO = new ShopcouponVO();
            shopcouponVO.setCouponid(coupon.getCouponid());
            shopcouponVO.setShopid(coupon.getShopid());
            shopcouponVO.setShopname(shopDao.selectById(coupon.getShopid()).getName());
            shopcouponVO.setFullminus(coupon.getFullminus());
            shopcouponVO.setDiscount(coupon.getDiscount());
            shopcouponVO.setStock(coupon.getStock());
            shopcouponVO.setAvailable(coupon.getAvailable());
            return shopcouponVO;
      }

      private int getMaxCouponId() {
            List<Integer> list = new ArrayList<>();
            for (Shopcoupon coupon : shopcouponDao.selectList(null)) {
                  list.add(coupon.getCouponid());
            }
            if (list.size() == 0)
                  return 0;
            return Collections.max(list);
      }
}
